package com.hf.healthy_life.controller;

import com.hf.healthy_life.pojo.HealthTaskInformation;
import com.hf.healthy_life.service.TaskService;
import com.hf.healthy_life.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/task")
public class TaskController {
    @Autowired
    private TaskService taskService;

    /*查询所有任务*/
    @GetMapping("/getTask")
    public Result getAll(Long userId){
        return Result.success(taskService.getAll(userId));
    }
    /* 添加任务*/
    @PostMapping("/addTask")
    public Result add(String taskName, Long userId){
        //封装任务
        HealthTaskInformation task = new HealthTaskInformation();
        task.setTaskName(taskName);
        task.setUserId(userId);
        task.setDate(new Date());//默认添加时间
        task.setIsDone("0");//默认未完成
        int result = taskService.add(task);
        return Result.success("添加成功");
    }

    /*删除任务*/
    @DeleteMapping("/deleteTask/{taskId}")
    public Result delete(@PathVariable("taskId") Long taskId){
        int result = taskService.delete(taskId);
        return Result.success("删除成功");
    }


    /*修改任务,主要是将任务状态设置为已完成*/
    @PostMapping("/editTask")
    public Result edit(Long taskId){
        HealthTaskInformation task = new HealthTaskInformation();
        task.setId(taskId);
        task.setIsDone("1");
        int result = taskService.update(task);
        return Result.success("修改成功");
    }


    /*计算当日任务完成进度*/
    @GetMapping("/calculateProgress")
    public Result calculateProgress(Long userId){
        return Result.success(taskService.calculateProgress(userId));
    }

    /*查询本星期任务完成情况*/
    @GetMapping("/getIfFinish")
    public Result getIfFinish(Long userId){

        return Result.success(taskService.getIfFinish(userId));
    }
    /*查询成就，也就是连续打卡了多少天*/
    @GetMapping("/getAchievement")
    public Result getAchievement(Long userId){
        return Result.success(taskService.getAchievement(userId));
    }
}
